package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/05
 */
@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public long queryFriendCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, Friend.class);
    }

    @Override
    public long queryLikeCount(long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public long queryFansCount(long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageResult<CommonLikeUser> findFriendList(int page, int pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page-1, pagesize));

        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        List<CommonLikeUser> userList = new ArrayList<>();
        for (Friend friend : friendList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(friend.getFriendId());
            user.setScore(queryRecommendScore(userId, user.getTargetUserId()));

            userList.add(user);
        }
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, userList);
    }

    @Override
    public PageResult<CommonLikeUser> findLikeList(int page, int pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page-1, pagesize));
        List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        List<CommonLikeUser> userList = new ArrayList<>();
        for (UserLike like : likeList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(like.getLikeUserId());
            user.setScore(queryRecommendScore(userId, user.getTargetUserId()));
            userList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, userList);
    }

    @Override
    public PageResult<CommonLikeUser> findFansList(int page, int pagesize, long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page-1, pagesize));
        List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        List<CommonLikeUser> userList = new ArrayList<>();
        for (UserLike like : likeList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(like.getUserId());
            user.setScore(queryRecommendScore(userId, user.getTargetUserId()));
            userList.add(user);
        }
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, userList);
    }

    @Override
    public PageResult<CommonLikeUser> findVisitorList(int page, int pagesize, long userId) {
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .with(PageRequest.of(page-1, pagesize));
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        long count = mongoTemplate.count(query, Visitor.class);

        List<CommonLikeUser> userList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(visitor.getVisitorUserId());
            user.setScore(queryRecommendScore(userId, user.getTargetUserId()));
            userList.add(user);
        }
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, userList);
    }

    @Override
    public void remove(long userId, long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    private Double queryRecommendScore(long toUserId, long targetUserId){
        Query query = new Query(Criteria.where("userId").is(targetUserId).and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            return 95D;
        }
        return recommendUser.getScore();
    }
    /**
     * 保存喜欢
     * @param userId 当前用户id
     * @param likeUserId 喜欢的用户id
     */
    @Override
    public void saveUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setLikeUserId(likeUserId);
        userLike.setUserId(userId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }
    /**
     * 查询喜欢用户
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public UserLike userList(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.findOne(query, UserLike.class);
    }

}
